<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row v-show="showSearch">
				<el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList">
					<el-form-item label="运营商" prop="name">
						<el-input v-model="state.queryForm.name" :placeholder="'请输入运营商'" clearable />
					</el-form-item>
					<el-form-item label="创建时间" prop="createTime">
						<el-date-picker
							:end-placeholder="$t('syslog.inputEndPlaceholderTip')"
							:start-placeholder="$t('syslog.inputStartPlaceholderTip')"
							range-separator="To"
							type="datetimerange"
							v-model="state.queryForm.createTime"
							value-format="YYYY-MM-DD HH:mm:ss"
						/>
					</el-form-item>
					<el-form-item>
						<el-button icon="Search" type="primary" @click="getDataList">{{ $t('common.queryBtn') }}</el-button>
						<el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button icon="plus" type="primary" class="ml10" @click="formDialogRef.openDialog()" v-auth="'platform_basicOperatorType_add'">
						新 增
					</el-button>
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'platform_basicOperatorType_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="运营商" show-overflow-tooltip />
				<el-table-column prop="state" label="状态" show-overflow-tooltip>
					<template #default="{ row }">
						<el-tag :type="row.state === 10 ? 'success' : 'danger'">{{ row.state === 10 ? '正常' : '已禁用' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="notes" label="备注" show-overflow-tooltip />
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip />
				<el-table-column label="操作" width="210">
					<template #default="scope">
						<el-button
							:type="scope.row.state === 10 ? 'danger' : 'success'"
							v-auth="'platform_basicOperatorType_enableOrDisable'"
							@click="handleSwitchState(scope.row)"
							>{{ scope.row.state === 10 ? '禁用' : '启用' }}</el-button
						>
						<el-button icon="edit-pen" text type="primary" v-auth="'platform_basicOperatorType_edit'" @click="formDialogRef.openDialog(scope.row.id)"
							>编辑</el-button
						>
						<el-button icon="delete" text type="danger" v-auth="'platform_basicOperatorType_del'" @click="handleDelete([scope.row.id])"
							>删除</el-button
						>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
		<!-- 启用、禁用 -->
		<switch-form-dialog ref="switchFormDialogRef" :api="enableOrDisable" @refresh="getDataList(false)" />
	</div>
</template>

<script setup lang="ts" name="systemBasicOperatorType">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs, enableOrDisable } from '/@/api/platform/basicOperatorType';
import { useMessage, useMessageBox } from '/@/hooks/message';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const SwitchFormDialog = defineAsyncComponent(() => import('/@/components/EnableOrDisable/index.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
const switchFormDialogRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/platform/basicOperatorType/export', Object.assign(state.queryForm, { ids: selectObjs }), 'basicOperatorType.xlsx');
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
// 启用禁用
const handleSwitchState = async (row: { id: string; state: number }) => {
	const text = row.state === 10 ? '禁用' : '启用';
	try {
		await useMessageBox().confirm(`您将对该运营商进行${text}操作，确定继续吗？`, false);
	} catch {
		return;
	}
	unref(switchFormDialogRef).openDialog(row.id, row.state === 10 ? 20 : 10, `${text}运营商`);
};
</script>
